﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GurLoader;
using GurLoaderHelper.Properties;

namespace GurLoaderHelper
{
    public class DefaultMenuProvider : IMenuProvider
    {
        public ContextMenuStrip CreateMenu(IEnumerable<string> selectedItemPaths)
        {
            //  Create the menu strip.
            var menu = new ContextMenuStrip();
            
            var sendParentItem = new ToolStripMenuItem
            {
                Text = "Send to Imgur", 
                Image = Resources.GurLoader16,
            };

            var sendImagesItem = new ToolStripMenuItem
            {
                Text = selectedItemPaths.Count() > 1 ? "As Images" : "As Image"
            };

            sendImagesItem.Click += (sender, e) => FireCommandGenerated(Command.SendImages, selectedItemPaths);

            var sendAlbumItem = new ToolStripMenuItem
            {
                Text = "As Album"
            };

            sendAlbumItem.Click += (sender, e) => FireCommandGenerated(Command.SendAlbum, selectedItemPaths);

            sendParentItem.DropDownItems.Add(sendImagesItem);
            sendParentItem.DropDownItems.Add(sendAlbumItem);

            //  Add the item to the context menu.
            menu.Items.Add(sendParentItem);

            //  Return the menu.
            return menu;
        }


        public bool CanShowMenu(IEnumerable<string> enumerable)
        {
            return true;
        }

        private void FireCommandGenerated(Command command, IEnumerable<string> paths)
        {
            if (CommandGenerated == null)
                return;

            StringBuilder sbParams = new StringBuilder();
            foreach (var path in paths)
            {
                sbParams.AppendFormat("\"{0}\" ", path);
            }

            string parameters = sbParams.ToString().Trim();

            CommandGenerated(this, new CommandGeneratedEventArgs(command, parameters));
        }

        public event EventHandler<CommandGeneratedEventArgs> CommandGenerated;
    }
}
